<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
         <!-- 搜索 -->
        <el-form :model="requestParameters" ref="requestParameters">
          <div class="filter-container">
           <el-form-item>
             学科名称 :
              <el-input
                style="width: 200px;"
                class="filter-item"
                v-model="requestParameters.subjectName"
              ></el-input>
              &nbsp;
              &nbsp;
              <el-button
                class="filter-item"
                size="small"
                type="primary"
                icon="el-icon-search"
                @click="serschSub"
              >搜索</el-button>
              <el-button class="filter-item" size="small" type="primary" @click="resetForm">重置</el-button>
            </el-form-item>
            <el-button
              class="filter-item fr"
              size="small"
              style="margin-left: 10px;"
              @click="handleChange('add')"
              type="primary"
              icon="el-icon-edit"
            >添加学科</el-button>
          </div>
        </el-form>
         <!-- 数据记录 -->
        <el-alert
          v-if="alertText !== ''"
          :title="alertText"
          type="info"
          class="alert"
          :closable="false"
          show-icon
        ></el-alert>
                <!-- 数据 -->
        <el-table
          v-loading="listLoading"
          element-loading-text="给我一点时间"
          :data="dataList"
          highlight-current-row
          style="width: 100%"
          border
        >
          <el-table-column align="center"  label="序号">
            <template slot-scope="scope">
              <span>{{scope.row.id}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"  label="学科名称">
            <template slot-scope="scope">
              <span>{{scope.row.subjectName}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"  label="创建者">
            <template slot-scope="scope">
              <span>{{scope.row.username}}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建日期" width="190" align="center">
            <template slot-scope="scope">
              <span>{{scope.row.addDate| parseTimeByString("{y}-{m}-{d} {h}:{m}:{s}")}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"  label="前台是否显示">
            <template slot-scope="scope">
              <span>{{scope.row.isFrontDisplay ? '是' : '否'}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"  label="二级目录">
            <template slot-scope="scope">
              <span>{{scope.row.twoLevelDirectory}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center"  label="标签">
            <template slot-scope="scope">
              <span>{{scope.row.tags}}</span>
            </template>
          </el-table-column>
            <el-table-column align="center"  label="题目数量">
            <template slot-scope="scope">
              <span>{{scope.row.totals}}</span>
            </template>
          </el-table-column>
          <!-- 操作按钮 -->
         <el-table-column
            align="center"
            label="操作"
            width="260"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope" class="Fsize">
               <el-link type="primary" :underline="false" :href="'#/subjects/directorys?id=' + scope.row.id +'&name=' +scope.row.subjectName" >学科分类</el-link>&nbsp;&nbsp;&nbsp;&nbsp;
               <el-link type="primary" :underline="false" :href="'#/subjects/tags?id=' + scope.row.id +'&name=' +scope.row.subjectName">学科标签</el-link>&nbsp;&nbsp;&nbsp;&nbsp;
               <el-link type="primary" :underline="false"  @click="handleChange(scope.row.id)">修改</el-link>&nbsp;&nbsp;&nbsp;&nbsp;
               <el-link type="primary" :underline="false"  @click="removeSub(scope.row.id)">删除</el-link>&nbsp;&nbsp;&nbsp;&nbsp;
              <!-- <el-button type="primary" size="mini" @click="handleChange(scope.row.id)">修改</el-button>              -->
              <!-- <el-button
                v-if="scope.row.status!='deleted'"
                size="mini"
                type="danger"
                @click="removeSub(scope.row.id)"
              >删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <!-- end -->
        <!-- 分页 -->
        <div class="pagination">
           <el-pagination
            background
              v-loading="listLoading"
              element-loading-text="拼命加载中"
              element-loading-spinner="el-icon-loading"
              @size-change="handleSizeChange"
              :hide-on-single-page="true"
              @current-change="handleCurrentChange"
              :current-page="Number(requestParameters.page)"
              :total="Number(total)"
              :page-size="Number(requestParameters.pagesize)"
              :page-sizes="[10,20,30,50]"
              layout="sizes, prev, pager, next, jumper"
          ></el-pagination>
        </div>
        <!-- end -->
         <!-- 新增标签弹层 -->
        <Dialog ref="editUser"
        :titleInfo="titleInfo" :formBase="formData" @newDataes="loadList"></Dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
// 导入弹出层
import Dialog from './../components/subjects-add'
import { list, remove, detail } from '@/api/hmmm/subjects'
export default {
  components: {
    Dialog
  },
  data () {
    return {
      alertText: '',
      listLoading: false,
      dataList: [],
      total: 200,
      requestParameters: {
        page: 1,
        pagesize: 10
      },
      titleInfo: {
        pageTitle: '新增学科', // 页面标题
        text: '' // 新增、编辑文本
      },
      formData: {
        isFrontDisplay: '',
        subjectName: ''
      }
    }
  },
  created () {
    this.loadList()
  },
  methods: {
    async loadList (params) {
      this.listLoading = true
      const { data: res } = await list(this.requestParameters)
      // 把数据给datalist赋值
      this.dataList = res.items
      this.listLoading = false
      this.total = res.counts
      this.alertText = `共 ${this.total} 条记录`
      // console.log(this.dataList)
    },
    // 每页显示信息条数
    handleSizeChange (val) {
      this.requestParameters.pagesize = val
      if (this.requestParameters.page === 1) {
        this.loadList(this.requestParameters)
      }
    },
    // 进入某一页
    handleCurrentChange (val) {
      this.requestParameters.page = val
      this.loadList()
    },
    // 重置
    resetForm () {
      this.requestParameters = {
        page: 1
      }
      this.loadList(this.requestParameters)
    },
    // 搜索
    serschSub () {
      this.requestParameters.page = 1
      this.loadList(this.requestParameters)
    },
    query () {
      this.formData = {}
    },
    // 新增用户、编辑用户
    handleChange (id) {
      this.query()
      this.$refs.editUser.dialogFormV()
      if (id === 'add') {
        this.titleInfo.text = '创建' // 给弹层传参
      } else {
        this.titleInfo.text = '编辑' // 给弹层传参
        this.hanldeEditForm(id)
      }
    },
    // 窗口操作**********************************
    // 编辑
    // 表单详情数据加载
    async hanldeEditForm (objeditId) {
      this.formData.id = objeditId
      const { data: res } = await detail({ id: objeditId })
      console.log(res)
      // 获取详情 给formData
      // if (res.isFrontDisplay === 1) {
      //   res.isFrontDisplay = true
      // } else {
      //   res.isFrontDisplay = false
      // }
      // 优化代码
      res.isFrontDisplay = !!res.isFrontDisplay
      this.formData = res
    },
    // 删除
    removeSub (id) {
      console.log(id)
      this.$confirm('此操作将永久删除 ' + ', 是否继续?', '提示', {
        type: 'warning'
      }).then(async () => {
        try {
          const res = await remove({ id })
          console.log(res)
          this.$message.success('删除成功!')
          this.loadList()
        } catch (error) {
          console.log(error)
          this.$message.error('删除失败!')
        }
      }).catch(() => {
      })
    }
  }
}
</script>
<style>
.el-table .el-link {
 font-size: 12px;
 }
</style>
